package uk.co.cdal.cobweb.frontend.GUI;


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.io.Serializable;
import java.util.LinkedList;

public class SpiderLayout 
implements LayoutManager, Serializable
{

	public void addLayoutComponent(String name, Component comp) {}
	
	private Dimension processComponent(Dimension size, 
			Container parent, 
			Component comp)
	{
		Insets insets = parent.getInsets();
		
		// Get bounds		
		double newW = insets.left + comp.getX() + comp.getWidth() + insets.right;
		double newH = insets.top + comp.getY() + comp.getHeight() + insets.bottom;
		
		double w = Math.max(size.getWidth(), newW);
		double h = Math.max(size.getHeight(), newH);
		
		size.setSize(w, h);
		
		return size;
	}

	public void layoutContainer(Container parent)
	{
		synchronized (parent.getTreeLock()) {
			
			int cMax = parent.getComponentCount();
			for (int c = 0; c < cMax; c++) {
				parent.getComponent(c).validate();
			}
		}
	}

	public Dimension minimumLayoutSize(Container parent)
	{
		Dimension size = new Dimension();
		int cCount = parent.getComponentCount();
		
		for (int c = 0; c < cCount; c++) {
			size = processComponent(size, parent, 
					parent.getComponent(c));
		}
		
		return size;
	}

	public Dimension preferredLayoutSize(Container parent)
	{
		return minimumLayoutSize(parent);
	}

	public void removeLayoutComponent(Component comp) {}        
}

